package cn.dida.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.model.Staff;
import cn.dida.model.UserSession;
import cn.dida.model.stastic.ProductScanCountByDate;
import cn.dida.service.StaffService;
import cn.dida.service.StasticService;

/**
 * 扫码统计
 * @author weiwei
 */
@Controller
public class HomeController extends BaseController {
    @Autowired
    private StaffService adminService;
    @Autowired
    private StasticService stasticService;

 //   @Autowired
 //   ZdyHandlerMethodMapping zdyHandlerMethodMapping;
    
    /**
     * 进入扫码统计页面
     * @return ModelAndView 扫码统计页面
     */
    @GetMapping("/home")
    public ModelAndView dashboard() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("name", "weiwei");
//        zdyHandlerMethodMapping.initHandlerMethods();
        return new ModelAndView("home").addAllObjects(model);
    }
    
    
    @Deprecated
    @GetMapping(value = "/admin/{account}")
    public ModelAndView get(HttpServletRequest request, @PathVariable("account") String account) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        Staff staff = adminService.getStaff(account, "");
        model.put("admin", staff);
        return new ModelAndView("admin").addAllObjects(model);
    }

    @Deprecated
    @ResponseBody
    @GetMapping(value = "/admin/{account}", produces = "application/json")
    public Object restGet(HttpServletRequest request, @PathVariable("account") String account) throws Exception {
        return adminService.getStaff(account, "");
    }

    /**
     * 商品扫码量统计
     * @param request
     * @param keyword 查询条件
     * @param timeLimitStart 扫码开始时间
     * @param timeLimitEnd 扫码结束时间
     * @param page 分页游标
     * @return HttpResponseBodyWrapper 包装商品扫码量数据
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/stastics/proscan")
    public Object stasticsProscan(HttpServletRequest request, String keyword, Long timeLimitStart, Long timeLimitEnd, Integer page) throws Exception {
        UserSession userSession = getUserSession(request);


        if (timeLimitStart == null || timeLimitEnd == null) {
            Calendar now = Calendar.getInstance();
            Calendar sevenDaysAgo = Calendar.getInstance();
            sevenDaysAgo.clear();
            sevenDaysAgo.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
            sevenDaysAgo.add(Calendar.DATE, -30);

            Calendar today = Calendar.getInstance();
            today.clear();
            today.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE), 23, 59, 59);
            today.set(Calendar.MILLISECOND, 999);

            timeLimitStart = sevenDaysAgo.getTimeInMillis();
            timeLimitEnd = today.getTimeInMillis();
        }

        Map<Integer, ArrayList<ProductScanCountByDate>> data = stasticService.stasticProductScanCountByDate(userSession.getCompanyId(), keyword, timeLimitStart, timeLimitEnd, 1);


        //补全缺省数据
//        Calendar startDate = Calendar.getInstance();
//        startDate.clear();
//        Calendar endDate = Calendar.getInstance();
//        endDate.clear();
//
//
//        List<Long> tmpList = new ArrayList<>();
//        for (ArrayList<ProductScanCountByDate> productScanCountByDateList : data.values()) {
//            for (ProductScanCountByDate productScanCountByDate : productScanCountByDateList) {
//                tmpList.add(productScanCountByDate.getScanDate());
//            }
//        }
//
//        Collections.sort(tmpList, Long::compareTo);
//
//
//        Calendar mindate = Calendar.getInstance();
//        mindate.setTimeInMillis(tmpList.get(0));
//        startDate.set(mindate.get(Calendar.YEAR), mindate.get(Calendar.MONTH), mindate.get(Calendar.DATE));
//
//        Calendar maxdate = Calendar.getInstance();
//        maxdate.setTimeInMillis(tmpList.get(tmpList.size() - 1));
//        endDate.set(maxdate.get(Calendar.YEAR), maxdate.get(Calendar.MONTH), maxdate.get(Calendar.DATE));
//
//
//        for (int i = 0; i < (endDate.getTimeInMillis() - startDate.getTimeInMillis()) / (1000 * 3600 * 24); i++) {
//
//            Calendar pointDate = Calendar.getInstance();
//            pointDate.clear();
//            pointDate.setTimeInMillis(startDate.getTimeInMillis());
//            pointDate.add(Calendar.DATE, i);
//
//
//            for (ArrayList<ProductScanCountByDate> productScanCountByDateList : data.values()) {
//                ProductScanCountByDate productScanCountByDate = productScanCountByDateList.get(i);
//                Calendar tmp = Calendar.getInstance();
//                tmp.setTimeInMillis(productScanCountByDate.getScanDate());
//
//
//                if (pointDate.compareTo(tmp) != 1) {
//                    ProductScanCountByDate defaultPoint = new ProductScanCountByDate();
//
//                    ProductScanCountByDate flag = productScanCountByDateList.get(0);
//                    defaultPoint.setProductId(flag.getProductId());
//                    defaultPoint.setProductName(flag.getProductName());
//                    defaultPoint.setScanCount(0l);
//                    defaultPoint.setScanDate(pointDate.getTimeInMillis());
//                    productScanCountByDateList.add(i, defaultPoint);
//                }
//
//
//            }
//        }


        return new HttpResponseBodyWrapper(data);
    }
}